package com.example.xutildemoproject.fragment;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.view.KeyEvent;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.example.xutildemoproject.activity.TestRouterActivity;
import com.xuexiang.constant.PermissionConstants;
import com.xuexiang.xaop.annotation.Permission;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xpage.base.XPageContainerListFragment;
import com.xuexiang.xpage.base.XPageSimpleListFragment;
import com.xuexiang.xpage.enums.CoreAnim;
import com.xuexiang.xpage.utils.TitleBar;
import com.xuexiang.xutil.app.IntentUtils;
import com.xuexiang.xutil.app.router.Router;
import com.xuexiang.xutil.common.ClickUtils;
import com.xuexiang.xutil.common.logger.Logger;
import com.xuexiang.xutil.system.CameraUtils;
import com.xuexiang.xutil.tip.ToastUtils;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

@Page(name = "XPush示例")
public class MainFragment extends XPageSimpleListFragment {
    private File mCameraFile;
    @Override
    protected List<String> initSimpleData(List<String> lists) {
        lists.add("路由测试");
        lists.add("通知");
        lists.add("社会化分享");
        lists.add("拍照");
        lists.add("url获取文件路径测试");
        lists.add("测试SAFUtils");
        lists.add("拨打电话:15634163385");
        return lists;
    }


    @Override
    protected TitleBar initTitleBar() {
        return super.initTitleBar().setLeftClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ClickUtils.exitBy2Click();
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            ClickUtils.exitBy2Click();
        }
        return true;
    }

    @Override
    protected void onItemClick(int position) {
        switch (position) {
            case 0:
                Map<String,Object> maps = new HashMap<>();
                maps.put("param1","我是参数1");
                maps.put("param2",123);
                Router.newIntent(this).to(TestRouterActivity.class)
                        .putBundleParam("param1","我是参数1")
                        .requestCode(100)
                        .launch();
                break;
            case 1:
                openPage(NotifyFragment.class,null, CoreAnim.slide);
                break;
            case 2:
                openPage(SocialShareFragment.class,null,CoreAnim.slide);
                break;
            case 3:
                openCamera();
                break;
            case 4:
                openPage(UriPathFragment.class,null,CoreAnim.slide);
                break;
            case 5:
                openPage(SAFFragment.class,null,CoreAnim.slide);
                break;
            case 6:
                callPhone();
            default:
                break;
        }
    }

    @Permission(PermissionConstants.CAMERA)
    private void openCamera() {
        CameraUtils.startOpenCamera(this, new CameraUtils.OnOpenCameraListener() {
            @Override
            public void onOpenCamera(File cameraFile) {
                mCameraFile = cameraFile;
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == CameraUtils.REQUEST_CAMERA) {
                ToastUtils.toast("文件路径：" + mCameraFile.getPath());
                Logger.i(mCameraFile.getPath());
            }else{
                ToastUtils.toast("请求码:" + requestCode + ", 返回码:" + resultCode + ", 返回内容:" + IntentUtils.getStringExtra(data,"back"));
            }
        }
    }

    @Permission(PermissionConstants.PHONE)
    private void callPhone(){
        startActivity(IntentUtils.getDialIntent("15634163385"));
    }
}
